package com.zhuiyun.pm.client.action.service.impl;

import com.zhuiyun.pm.client.action.entity.GoodsEarnestMoneyEntity;
import com.zhuiyun.pm.client.action.entity.GoodsEntity;
import com.zhuiyun.pm.client.action.mapper.GoodsEarnestMoneyMapper;
import com.zhuiyun.pm.client.action.mapper.GoodsMapper;
import com.zhuiyun.pm.client.action.service.AuctionService;
import com.zhuiyun.pm.client.bid.util.StringUtils;
import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.utils.PrimaryKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AuctionServiceImpl
 * @Description TODO 拍卖 service层实现类
 * @Author 赵笛
 * @Date 2023/3/14 14:05
 **/
@Service
public class AuctionServiceImpl implements AuctionService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsEarnestMoneyMapper goodsEarnestMoneyMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    /**
    * @Author 86182
    * @Description TODO 根据（地址）（状态）查询竞品信息列表
    * @Date 2023/3/14 14:11
    * @Return java.util.List<com.zhuiyun.pm.client.goods.entity.GoodsEntity>
    * @param address
    * @param status
    * @param pageRequest
    */
    @Override
    public List<GoodsEntity> getGoodsList(String goodsName,String address, String status, PageRequest pageRequest) {
        return goodsMapper.getGoodsList(goodsName,address,status,pageRequest);
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据当前登录用户id查询用户与商品的保证金信息
     * @Date 2023/3/14 20:42
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param userId
     */
    @Override
    public CommonResult getGoodsEarnestMoneyByUserId(Integer userId,Integer goodsId) {
        if((userId!=null&&userId!=0)&&(goodsId!=null&&goodsId!=0)){
            GoodsEarnestMoneyEntity goods = goodsEarnestMoneyMapper.getGoodsEarnestMoneyByUserId(userId,goodsId);
            if(goods!=null){
                return CommonResult.ok(goods);
            }else{
                return CommonResult.error("当前用户暂未参与此商品的拍卖");
            }
        }else{
            return CommonResult.error("用户未登录或商品不存在");
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO
     * @Date 2023/3/16 15:38
     * @Return
     * @param goodsId
     */
    @Override
    public CommonResult getGoodsEarnestMoneyCount(Integer goodsId) {
        if(goodsId!=null&&goodsId!=0){
            Integer count = goodsEarnestMoneyMapper.getGoodsEarnestMoneyCount(goodsId);
            return CommonResult.ok(count);
        }else{
            return CommonResult.error("用户未登录或商品不存在");
        }
    }
    /**
     * @Author 梁栋
     * @Description TODO 根据用户id分页查询商品列表
     * @Date 2023/3/16 15:42
     * @Return com.zhuiyun.pm.config.CommonResult
     * @Param [java.lang.String]
     */
    @Override
    public Object getGoodsByUserIdLimit6(Integer userId) {
        Example example = new Example(GoodsEntity.class);
        example.setOrderByClause(" id limit 0,6");
        example.createCriteria().andEqualTo("sellerId",userId).andEqualTo("checkStatus",5);
        return goodsMapper.selectByExample(example);
    }
    /**
     * @description:app查询审核状态5的goods
     * @author: dongdong
     * @date: 2023/3/16 16:19
     * @param: [status]
     * @return: java.util.List<com.zhuiyun.pm.client.action.entity.GoodsEntity>
    **/
    @Override
    public List<GoodsEntity> getGoodsListNotPage(String status) {
        Example example = new Example(GoodsEntity.class);
        example.setOrderByClause(" RAND()");
        Example.Criteria criteria = example.createCriteria().andEqualTo("checkStatus", 5).andNotEqualTo("transactionStatus", 3);
        if (StringUtils.isNotEmpty(status)){
            criteria.andEqualTo("transactionStatus",status);
        }
        return goodsMapper.selectByExample(example);
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据用户id查询当前用户
     * @Date 2023/3/23 15:00
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param goodsId
     */
    @Override
    public CommonResult getGoodsById(Integer goodsId) {
        GoodsEntity goodsEntity = goodsMapper.selectByPrimaryKey(goodsId);
        if(goodsEntity!=null){
            return CommonResult.ok(goodsEntity);
        }else{
            return CommonResult.error("暂无当前竞品信息");
        }
    }
    /**
     * @description:app查询商品的类型
     * @author: 高
     * @date: 2023/3/23 09:57
     * @param: [auctionType]
     **/
    @Override
    public List<GoodsEntity> getGoodsListType(String auctionType) {
        Example example = new Example(GoodsEntity.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("checkStatus", 5).andNotEqualTo("transactionStatus", 3);
        if (StringUtils.isNotEmpty(auctionType)){
            criteria.andEqualTo("auctionType", auctionType);
        }
        return goodsMapper.selectByExample(example);
    }
    /**
     * @Author 赵笛
     * @Description TODO 生成当前用户的缴纳保证金状态
     * @Date 2023/3/27 18:52
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param goodsEarnestMoneyEntity
     */
    @Override
    public CommonResult generateGoodsEarnestMoney(GoodsEarnestMoneyEntity goodsEarnestMoneyEntity) {
        goodsEarnestMoneyEntity.setEarnestMoneyCode(primaryKeyUtil.getEmCode());
        goodsEarnestMoneyEntity.setEarnestMoneyStatus(0);
        goodsEarnestMoneyEntity.setTransactionStatus(0);
        goodsEarnestMoneyEntity.setCreateTime(new Date());
        goodsEarnestMoneyEntity.setUpdateTime(new Date());
        goodsEarnestMoneyEntity.setDelFlag(0);
        int i = goodsEarnestMoneyMapper.insertSelective(goodsEarnestMoneyEntity);
        if(i>=0){
            return CommonResult.ok("生成成功");
        }else{
            return CommonResult.error("生成失败");
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 修改保证金状态
     * @Date 2023/3/28 11:24
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param goodsEarnestMoneyEntity
     */
    @Override
    public CommonResult updateEarnestMoneyStatus(GoodsEarnestMoneyEntity goodsEarnestMoneyEntity) {
        int i = goodsEarnestMoneyMapper.updateEarnestMoneyStatus(goodsEarnestMoneyEntity);
        if(i>=0){
            return CommonResult.ok("支付成功");
        }else{
            return CommonResult.error("支付失败");
        }
    }

    /**
     * @Author 赵笛
     * @Description TODO 根据当前登录用户（卖家）和审核状态查询竞品列表
     * @Date 2023/3/29 19:08
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param sellerId
     * @param checkStatus
     * @param goodsName
     */
    @Override
    public CommonResult getGoodsListBySellerIdAndStatus(Integer sellerId, Integer checkStatus, String goodsName) {
        if(sellerId!=null){
            List<GoodsEntity> list = goodsMapper.getGoodsListBySellerIdAndStatus(sellerId,checkStatus,goodsName);
            if(!list.isEmpty()){
                return CommonResult.ok(list);
            }else{
                return CommonResult.error("当前卖家暂无该状态竞品");
            }
        }else {
            return CommonResult.error("当前用户未登录！！！");
        }
    }
    /**
     * @Author 赵笛
     * @Description TODO 根据id删除竞品
     * @Date 2023/3/30 16:50
     * @Return com.zhuiyun.pm.config.CommonResult
     * @param goodsId
     */
    @Override
    public CommonResult deleteGoodsById(Integer goodsId) {
        int i = goodsMapper.deleteGoodsById(goodsId);
        if(i>0){
            return CommonResult.ok("删除成功");
        }else{
            return CommonResult.error("删除失败");
        }
    }


}
